package framework.po;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class POTestCase {

    public String name;
    public List<HashMap<String, Object>> before_all;
    public List<HashMap<String, Object>> after_all;
    public List<HashMap<String, Object>> steps;

    public void run() {

        AtomicReference<By> default_by = new AtomicReference<>();
        AtomicReference<POBasePage> lastPage = new AtomicReference<>();

        steps.forEach(step -> {
                    step.entrySet().forEach(entry -> {
                                String action = entry.getKey().toLowerCase();
                                String[] keyArray = action.split("\\.");
                                Object value = entry.getValue();

                                if (keyArray.length > 1) {
                                    String poName = keyArray[0];
                                    String poMethod = keyArray[1];

                                    System.out.println(String.format("%s.%s %s", poName, poMethod, value));


                                    if (poMethod.equals("new")){
                                        POBasePage currentPage=null;
                                        if (lastPage.get()==null){
                                            currentPage=POBasePage.load((String) value,null);
                                        }else {
                                            currentPage=POBasePage.load((String) value,lastPage.get().driver);
                                        }

                                        lastPage.set(currentPage);
                                        POStore.getInstance().setPO(poName,currentPage);
                                    }else{
                                        POStore.getInstance().getPO(poName).runPOMethod(poMethod);
                                    }

                                } else {

                                    System.out.println(String.format("%s %s", action, value));

                                }


                            }

                    );


                }

        );


    }
}
